class Solution {
   public:
    int reverse(int x) {
        int f = 1;
        if (x == INT_MIN) return 0;
        if (x < 0) {
            x = -x;
            f = -1;
        }
        int num = 0;
        while (x) {
            if (num > INT_MAX / 10) return 0;
            num = num * 10 + x % 10;
            x /= 10;
        }
        return num * f;
    }
};